// pages/orderdetail/orderdetail.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // num: "201801131230519990001",
    shoplist: '',
    shi:"",
    fen:"",
    miao:"",
    nr:"",
    showyuanyin:false
  },

  tosupermarketdetails: function (e) {
    var shopID = e.currentTarget.dataset.shopid
    var ID = e.currentTarget.dataset.id
    

    wx.navigateTo({
      url: '/pages/supermarket-details/details?shopID=' + shopID + "&ID=" + ID,
    })
  },

  tcqxdd: function () {
    this.setData({
      showyuanyin: false
    })
  },


  getlist: function () {
    ajax('/api/OrderCareful/getConsumerCareful?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      PersonID: wx.getStorageSync('PersonID'),
      ID: this.data.orderid
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {
        console.log(res)
        this.setData({
          shoplist: res.Models
        })

        if (res.Models[0].BuildFlag == 0 || res.Models[0].BuildFlag == 1){
          var timelist = res.Models[0].DJS.split(":")
          this.setData({
            shi: Number(timelist[0]),
            fen: Number(timelist[1]),
            miao: Number(timelist[2]),
          })

          // this.settime()
        }

        this.jisuanallmoney()
      })
  },

  topingodetails:function(){
    wx.navigateTo({
      url: '/pages/detailsofpurchase/detailsofpurchase?orderid=' + this.data.shoplist[0].ID,
    })
  },

  inputnum: function (e) {
    // console.log(e.detail.value)
    var zishu = e.detail.value.length
    this.setData({
      zishu: zishu,
      nr: e.detail.value
    });
  },

  showyuanyin:function(){
    // this.setData({
    //   showyuanyin:true
    // })
    this.quxiaoorder()
  },

  sleep(delay) {
    var start = (new Date()).getTime();
    while ((new Date()).getTime() - start < delay) {
      continue;
    }
  },

  settime: function () {
    var that = this
    that.timer = setInterval(function () {
      var miao = that.data.miao -= 1
      var fen = that.data.fen
      var shi = that.data.shi
      if (miao <= 0) {
        fen -= 1
        miao = 60
      }
      if (fen == -1) {
        shi -= 1
        fen = 60
      }

      that.setData({
        miao: miao,
        fen: fen,
        shi: shi
      })

      if(shi==-1){
        clearInterval(that.timer)
        console.log("计时结束")
        that.sleep(3000)
        that.getlist()
      }

      
    }, 1000)
  },

  tohelpself:function(){
    wx.reLaunch({
      url: '/pages/selfhelp/selfhelp?ordercode=' + this.data.shoplist[0].iCode,
    })
  },
// 客服电话
  phoneCall: function (e) {
    var tel = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: tel //仅为示例，并非真实的电话号码
    })
  },

  toshophomepage: function (e) {

    wx.navigateTo({
      url: '/pages/shop-homepage/homepage?ID=' + e.currentTarget.dataset.id,
    })
  },

  jisuanallmoney:function(){
    var allmsg = this.data.shoplist[0].Product
    var allmoney=0
    for (var x in allmsg){
      allmoney=allmoney*100
      var danjia = allmsg[x].DMoney*100
      var danjiaall = danjia * allmsg[x].OrderNumber

      allmoney = allmoney + danjiaall
      allmoney=allmoney/100
    }
    this.setData({
      allmoney:allmoney
    })

    // 商品总价
    this.data.shoplist[0].IMoney
    //运费
    this.data.shoplist[0].PSMoney

    this.setData({
      yunfei: this.data.shoplist[0].PSMoney
    })
    // 起送费
    this.data.shoplist[0].QSMoney
    // 免配送金额
    this.data.shoplist[0].MPSMoney
    if (allmoney >= this.data.shoplist[0].MPSMoney){
      this.setData({
        yunfeiyouhui: this.data.shoplist[0].PSMoney
      })
    }else{
      this.setData({
        yunfeiyouhui: 0
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var orderid=options.orderid
    this.setData({
      orderid: orderid
    })
    this.getlist()
  },

  copy:function(){
    var that = this;
    console.log(that.data.num)
    wx.setClipboardData({
      data: that.data.shoplist[0].iCode,
      success(res) {
        console.log("copy")
      },fail(res){
        console.log(res)
      }
    })
  },

  // 详情功能

  topay:function(){
    wx.navigateTo({
      url: '/pages/pay/pay?orderid=' + this.data.shoplist[0].ID + '&ordercode=' + this.data.shoplist[0].iCode + '&OrderType=' + this.data.shoplist[0].VType,
    })
  },

  quxiaoorder:function(){

    // this.setData({
    //   showyuanyin: false
    // })

    ajax('/api/OrderCareful/GetCancelOrder?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      OrderID: this.data.shoplist[0].ID,
      iCode: this.data.shoplist[0].iCode,
      lbltext:this.data.nr,
      BuildFlag: this.data.shoplist[0].BuildFlag
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {
        console.log(res)
        if (res.Status== 1){

          wx.showToast({
            title: '提交成功',
            icon: 'none',
            duration: 2000
          })

          this.getlist()
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})